package com.yanqu.road.dao.impl.activity.witchdraw;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.witchdraw.data.WitchDrawUserData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;

public class WitchDrawUserDaoImpl extends TempDataObjectDao<WitchDrawUserData> {


    public static String getWitchDrawUserDataSql = "select * from `t_u_witch_draw_user_data` where `activityId`=? and `userId`=?";
    public WitchDrawUserData getWitchDrawUserData(int activityId,long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getWitchDrawUserDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setLong(i++, userId);
            rs = call.executeQuery();
            if (rs.next()) {
                return parse(rs);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    public static String updateWitchDrawUserDataSql = "update `t_u_witch_draw_user_data` set score=?,simpleEnsureTimes=?,mythEnsureTimes=?,totalDrawTimes=?,freeDrawTimes=?,mythTotalDrawTimes=? where `activityId`=? and `userId`=?";
    @Override
    protected boolean updateData(WitchDrawUserData witchDrawUserData) {
        witchDrawUserData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, witchDrawUserData.getScore()));
        para.put(i++, new DbParameter(Types.INTEGER, witchDrawUserData.getSimpleEnsureTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, witchDrawUserData.getMythEnsureTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, witchDrawUserData.getTotalDrawTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, witchDrawUserData.getFreeDrawTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, witchDrawUserData.getMythTotalDrawTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, witchDrawUserData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, witchDrawUserData.getUserId()));
        if (getDb().executeNoneQuery(updateWitchDrawUserDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertWitchDrawUserDataSql = "insert into `t_u_witch_draw_user_data`(activityId,userId,score,simpleEnsureTimes,mythEnsureTimes,totalDrawTimes,freeDrawTimes,mythTotalDrawTimes) values (?,?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(WitchDrawUserData witchDrawUserData) {
        witchDrawUserData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, witchDrawUserData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, witchDrawUserData.getUserId()));
        para.put(i++, new DbParameter(Types.BIGINT, witchDrawUserData.getScore()));
        para.put(i++, new DbParameter(Types.INTEGER, witchDrawUserData.getSimpleEnsureTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, witchDrawUserData.getMythEnsureTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, witchDrawUserData.getTotalDrawTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, witchDrawUserData.getFreeDrawTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, witchDrawUserData.getMythTotalDrawTimes()));
        if (getDb().executeNoneQuery(insertWitchDrawUserDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected WitchDrawUserData parseData(ResultSet rs) throws SQLException {
        WitchDrawUserData witchDrawUserData = new WitchDrawUserData();
        witchDrawUserData.setActivityId(rs.getInt("activityId"));
        witchDrawUserData.setUserId(rs.getLong("userId"));
        witchDrawUserData.setScore(rs.getLong("score"));
        witchDrawUserData.setSimpleEnsureTimes(rs.getInt("simpleEnsureTimes"));
        witchDrawUserData.setMythEnsureTimes(rs.getInt("mythEnsureTimes"));
        witchDrawUserData.setTotalDrawTimes(rs.getInt("totalDrawTimes"));
        witchDrawUserData.setFreeDrawTimes(rs.getInt("freeDrawTimes"));
        witchDrawUserData.setMythTotalDrawTimes(rs.getInt("mythTotalDrawTimes"));

        return witchDrawUserData;
    }

}
